﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotSpatial.Controls;
using DotSpatial.Controls.Header;
using ShadingTab.Properties;
using System.Windows.Forms;
using DotSpatial.Data;
using DotSpatial.Topology;
using System.Drawing;
using DotSpatial.Symbology;
using System.Data.OleDb;
using System.Data;
using System.Collections;
using System.IO;

using DotSpatial.Projections;
using System.Xml;
using System.Globalization;

namespace ShadingTab
{
    class Class3dExport
    {
        string temp_path = Path.GetDirectoryName(System.IO.Path.GetTempFileName());
        string pvDir = "C:\\Users\\Rohan\\Documents\\" ; //System.IO.Directory.GetCurrentDirectory();

        public ProjectVariables prj;
        public IMap map;
        public UtilityFunctions util;

        int getSketchUpId(int i)
        {
            if (i == 0) { return 2; }
            else { return 10 + (i - 1) * 6; }
        }

        int getSketchUpSourceId(int i)
        {
            return 5 + (i) * 6;
        }

        private void Export2SketchUp4Tree(IMapFeatureLayer treeFe, string exportfolder)
        {
            IMapRasterLayer demLyr;
            Raster dem4Pv = new Raster();
            double z0 = 0;
            int iDemLyr = -1; // util.getLayerHdl(prj.LyrDEMname, map);
            if (iDemLyr != -1 & prj.DemChecked == true)
            {
                demLyr = map.Layers[iDemLyr] as IMapRasterLayer;
                int mRow = demLyr.Bounds.NumRows;
                int mCol = demLyr.Bounds.NumColumns;
                dem4Pv = (Raster)demLyr.DataSet;
                Coordinate ptReference = new Coordinate(Convert.ToDouble(prj.UtmE), Convert.ToDouble(prj.UtmN));
                RcIndex rc = dem4Pv.ProjToCell(ptReference);
                //double z0 = demLyr.DataSet.Value[rc.Row, rc.Column];
                if (rc.Column < 0 | rc.Row < 0)
                {
                    z0 = 0;
                }
                else
                {
                    z0 = dem4Pv.Value[rc.Row, rc.Column];
                }
            }

            int nShp = treeFe.DataSet.NumRows() - 1;
            //Extent ext = treeFe.DataSet.GetFeature(nShp).Envelope.ToExtent();
            /*
            for (int i = 0; i < treeFe.DataSet.NumRows(); i++)
            {
                IFeature fs = treeFe.DataSet.GetFeature(i);
                double x1 = fs.Coordinates[0].X;
                double y1 = fs.Coordinates[0].Y;
            }
             */
            int nTree = treeFe.DataSet.NumRows() - 1;
            //
            //int nNode = 40;
            int i = 0;
            //double y0 = 10;
            //double x0 = 0;
            double ii = 0;
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            //object obj;
            //XmlWriter writer = XmlWriter.Create(pvDir + "\\SketchUp\\temp.xml", settings); temp_path
            XmlWriter writer = XmlWriter.Create(temp_path + "\\temp.xml", settings);

            //-------------------------------------------------------
            //Header
            //-------------------------------------------------------
            writer.WriteStartElement("COLLADA");
            {
                writer.WriteAttributeString("xmlns", "xsi", null, "http://www.collada.org/2005/11/COLLADASchema");
                writer.WriteAttributeString("version", "1.4.1");
                writer.WriteStartElement("asset");
                {
                    writer.WriteStartElement("contributor");
                    {
                        writer.WriteStartElement("authoring_tool");
                        {
                            writer.WriteString("SketchUp 8.0.16846");
                        } writer.WriteEndElement(); //"authoring_tool"
                    } writer.WriteEndElement(); //"contributor"
                    writer.WriteStartElement("created");
                    {
                        writer.WriteString("2013-07-22T15:46:40Z");
                    } writer.WriteEndElement(); //"created"                    
                    writer.WriteStartElement("modified");
                    {
                        writer.WriteString("2013-07-22T15:46:40Z");
                    } writer.WriteEndElement(); //"modified"
                    writer.WriteStartElement("unit");
                    {
                        writer.WriteAttributeString("meter", "0.2539999969303608");
                        writer.WriteAttributeString("name", "inch");
                    } writer.WriteEndElement(); //"unit"
                    writer.WriteStartElement("up_axis");
                    {
                        writer.WriteString("Z_UP");
                    } writer.WriteEndElement(); //"up_axis"                    
                } writer.WriteEndElement(); //"asset"
                writer.WriteStartElement("library_visual_scenes");
                {
                    writer.WriteStartElement("visual_scene");
                    {
                        writer.WriteAttributeString("id", "ID1");
                        //--------------------------------------------------------------------------
                        // Node
                        //--------------------------------------------------------------------------
                        writer.WriteStartElement("node");
                        {
                            writer.WriteAttributeString("name", "SketchUp");
                            //Number of trees
                            i = 0;
                            //for (int t = 0; t < nTree; t++)
                            for (int t = 0; t < treeFe.DataSet.NumRows(); t++)
                            {
                                for (int iTree = 1; iTree <= 40; iTree++)
                                {
                                    i++;// = iTree;
                                    writer.WriteStartElement("instance_geometry");
                                    {
                                        writer.WriteAttributeString("url", "#ID" + getSketchUpId(i).ToString());
                                        writer.WriteStartElement("bind_material");
                                        {
                                            writer.WriteStartElement("technique_common");
                                            {
                                                writer.WriteStartElement("instance_material");
                                                {
                                                    writer.WriteAttributeString("symbol", "Material2");
                                                    writer.WriteAttributeString("target", "#ID3");
                                                    writer.WriteStartElement("bind_vertex_input");
                                                    {
                                                        writer.WriteAttributeString("semantic", "UVSET0");
                                                        writer.WriteAttributeString("input_semantic", "TEXCOORD");
                                                        writer.WriteAttributeString("input_set", "0");
                                                    } writer.WriteEndElement();
                                                } writer.WriteEndElement();
                                            } writer.WriteEndElement();
                                        } writer.WriteEndElement();
                                    } writer.WriteEndElement();
                                }
                            }
                        } writer.WriteEndElement();//"Node"
                        //--------------------------------------------------------------------------
                    } writer.WriteEndElement(); //"visual_scene"
                } writer.WriteEndElement(); //"library_visual_scenes"


                //----------------------------------------------------------------------------------
                // BEGIN Geometry
                //----------------------------------------------------------------------------------
                writer.WriteStartElement("library_geometries");
                //-----------------------------------------------------------------------------------
                //-----------------------------------------------------------------------------------
                i = 0;
                //for (int t = 0; t < nTree; t++)
                double ToMeter = 3.96874995203687; // Convert To Meter for sketchUp;
                for (int t = 0; t < treeFe.DataSet.NumRows(); t++)
                {
                    IFeature fs = treeFe.DataSet.GetFeature(t);
                    double x0 = prj.UtmE;// Convert.ToDouble(txtUtmE.Text);
                    double y0 = prj.UtmN;// Convert.ToDouble(txtUtmN.Text);


                    double x1 = (fs.Coordinates[0].X - x0) * ToMeter;
                    double y1 = (fs.Coordinates[0].Y - y0) * ToMeter;
                    object objDiameter = fs.DataRow["diameter"];
                    object objHeight = fs.DataRow["height"];
                    object objType = fs.DataRow["type"];
                    double treeRadius = (double)objDiameter / 2;
                    double treeHeight = (double)objHeight;
                    int treeTypeId = getTreeTypeId((string)objType);

                    for (int iTree = 1; iTree <= 40; iTree++)
                    {
                        i++;// = iTree;
                        writer.WriteStartElement("geometry");
                        {
                            writer.WriteAttributeString("id", "ID" + getSketchUpId(i).ToString());
                            writer.WriteStartElement("mesh");
                            {
                                //--------
                                //1st Set
                                //--------
                                writer.WriteStartElement("source");
                                {
                                    writer.WriteAttributeString("id", "ID" + getSketchUpSourceId(i).ToString());
                                    writer.WriteStartElement("float_array");
                                    {
                                        writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 3).ToString());
                                        writer.WriteAttributeString("count", "72");
                                        //writer.WriteString(get3Dcord(i));
                                        //TreeRadius[t] = 10;
                                        //TreeHeight[t] = 15;
                                        //treeForm[t] = 1;
                                        //treeE[t] = 15;
                                        //treeN[t] = 0;
                                        double radius = treeRadius; //TreeRadius[t];
                                        int tForm = treeTypeId;// treeForm[t];
                                        double H = treeHeight;
                                        double r = treeRedius((double)iTree / 40, tForm) * radius;
                                        double z = 0;
                                        if (iDemLyr != -1 & prj.DemChecked == true)
                                        {
                                            Coordinate ptReference = new Coordinate(fs.Coordinates[0].X, fs.Coordinates[0].Y);
                                            RcIndex rc = dem4Pv.ProjToCell(ptReference);
                                            if (rc.Column < 0 | rc.Row < 0)
                                            {
                                                z = z0;
                                            }
                                            else
                                            {
                                                //double z0 = demLyr.DataSet.Value[rc.Row, rc.Column];
                                                z = dem4Pv.Value[rc.Row, rc.Column] - z0;
                                            }
                                        }
                                        writer.WriteString(GeoTree4SketchUp(r, H * ToMeter, x1, y1, z + H * (iTree - 1) / 40 * (3.96874995203687)));

                                    } writer.WriteEndElement();//"float_array"    

                                    writer.WriteStartElement("technique_common");
                                    {
                                        writer.WriteStartElement("accessor");
                                        {// <accessor count="24" source="#ID8" stride="3">
                                            writer.WriteAttributeString("count", "24");
                                            writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 3).ToString());
                                            writer.WriteAttributeString("stride", "3");
                                            //<param name="X" type="float" />
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "X");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Y");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Z");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement(); //"param"
                                        } writer.WriteEndElement(); //"accessor"
                                    } writer.WriteEndElement(); //"technique_common"
                                } writer.WriteEndElement();//"source"
                                //--------
                                //2nd Set
                                //--------
                                writer.WriteStartElement("source");
                                {
                                    writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 1).ToString());
                                    writer.WriteStartElement("float_array");
                                    {
                                        writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 4).ToString());
                                        writer.WriteAttributeString("count", "72");
                                        writer.WriteString("0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1");
                                        //writer.WriteString(get3DCam(1));
                                    } writer.WriteEndElement();//"float_array"    

                                    writer.WriteStartElement("technique_common");
                                    {
                                        writer.WriteStartElement("accessor");
                                        {// <accessor count="24" source="#ID8" stride="3">
                                            writer.WriteAttributeString("count", "24");
                                            writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 4).ToString());
                                            writer.WriteAttributeString("stride", "3");
                                            //<param name="X" type="float" />
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "X");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Y");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Z");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement(); //"param"
                                        } writer.WriteEndElement(); //"accessor"
                                    } writer.WriteEndElement(); //"technique_common"
                                } writer.WriteEndElement();//"source"
                                //--------
                                //3rd Set
                                //--------
                                writer.WriteStartElement("vertices");
                                {
                                    writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 2).ToString());
                                    writer.WriteStartElement("input");
                                    {// <input semantic="POSITION" source="#ID11" />
                                        writer.WriteAttributeString("semantic", "POSITION");
                                        writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 0).ToString());
                                    } writer.WriteEndElement(); //"input"
                                    writer.WriteStartElement("input");
                                    {// <input semantic="NORMAL" source="#ID12" />
                                        writer.WriteAttributeString("semantic", "NORMAL");
                                        writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 1).ToString());
                                    } writer.WriteEndElement(); //"input"
                                } writer.WriteEndElement();//"vertices"
                                writer.WriteStartElement("triangles");
                                {//<triangles count="12" material="Material2">
                                    writer.WriteAttributeString("count", "22");
                                    writer.WriteAttributeString("material", "Material2");
                                    writer.WriteStartElement("input");
                                    {//<input offset="0" semantic="VERTEX" source="#ID13" />
                                        writer.WriteAttributeString("offset", "0");
                                        writer.WriteAttributeString("semantic", "VERTEX");
                                        writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 2).ToString());
                                    } writer.WriteEndElement(); //"input"
                                    writer.WriteStartElement("p");
                                    {//
                                        writer.WriteString("0 1 2 1 0 3 3 0 4 3 4 5 5 4 6 5 6 7 7 6 8 7 8 9 9 8 10 9 10 11 11 10 12 11 12 13 13 12 14 13 14 15 15 14 16 15 16 17 17 16 18 17 18 19 19 18 20 19 20 21 21 20 22 21 22 23");
                                    } writer.WriteEndElement(); //"p"

                                } writer.WriteEndElement();//"triangles"                        
                            } writer.WriteEndElement();//"mesh"
                        } writer.WriteEndElement(); //"geometry"
                    }
                } writer.WriteEndElement();//library_geometries
                //-----------------------------------------------------------------------------------
                // END GEOMETRY
                //-----------------------------------------------------------------------------------
                //-----------------------------------------------------------------------------------


                //----------------------------------------------------------------------------------
                //Footer
                //----------------------------------------------------------------------------------
                writer.WriteStartElement("library_materials");
                {
                    writer.WriteStartElement("material");
                    {
                        writer.WriteAttributeString("id", "ID3");
                        writer.WriteAttributeString("name", "__auto_");
                        writer.WriteStartElement("instance_effect");
                        {
                            writer.WriteAttributeString("url", "#ID4");
                        } writer.WriteEndElement(); //"instance_effect"
                    } writer.WriteEndElement(); //"material"
                } writer.WriteEndElement(); //"library_materials"

                writer.WriteStartElement("library_effects");
                {
                    writer.WriteStartElement("effect");
                    {
                        writer.WriteAttributeString("id", "ID4");
                        writer.WriteStartElement("profile_COMMON");
                        {
                            writer.WriteStartElement("technique");
                            {
                                writer.WriteAttributeString("sid", "COMMON");
                                writer.WriteStartElement("lambert");
                                {
                                    writer.WriteStartElement("diffuse");
                                    {
                                        writer.WriteStartElement("color");
                                        {
                                            writer.WriteString("1 1 1 1");

                                        } writer.WriteEndElement(); //"color"   
                                    } writer.WriteEndElement(); //"diffuse"   
                                } writer.WriteEndElement(); //"lambert"      
                            } writer.WriteEndElement(); //"technique"      
                        } writer.WriteEndElement(); //"profile_COMMON"       
                    } writer.WriteEndElement(); //"effect"                
                } writer.WriteEndElement(); //"library_effects"

                writer.WriteStartElement("scene");
                {
                    writer.WriteStartElement("instance_visual_scene");
                    {
                        writer.WriteAttributeString("url", "#ID1");

                    } writer.WriteEndElement(); //"instance_visual_scene"   
                } writer.WriteEndElement(); //"scene"   
            } writer.WriteEndElement(); //"COLLADA"
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();



            String line;
            try
            {
                StreamReader sh = new StreamReader(pvDir + "\\SketchUp\\COLLADASH.h");
                //StreamReader sr = new StreamReader(pvDir + "\\SketchUp\\temp.xml");
                StreamReader sr = new StreamReader(temp_path + "\\temp.xml");
                StreamWriter sw = new StreamWriter(exportfolder + "\\treeModel.dae");
                line = sh.ReadLine(); sw.WriteLine(line); line = sr.ReadLine();
                line = sh.ReadLine(); sw.WriteLine(line); line = sr.ReadLine();
                line = sr.ReadLine();
                while (line != null)
                {
                    sw.WriteLine(line);
                    line = sr.ReadLine();
                }

                //close the file
                sh.Close();
                sr.Close();
                sw.Close();
                //MessageBox.Show("ok");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }

        }

        private void Export2SketchUp4Bldg(IMapFeatureLayer bldgFe, string exportfolder)
        {
            int nShp = bldgFe.DataSet.NumRows() - 1;
            //Extent ext = poleFe.DataSet.GetFeature(nShp).Envelope.ToExtent();
            double x0 = prj.UtmE;// Convert.ToDouble(txtUtmE.Text);
            double y0 = prj.UtmN;// Convert.ToDouble(txtUtmN.Text);
            double z0 = 0;
            IMapRasterLayer demLyr;
            Raster dem4Pv = new Raster();
            int iDemLyr = -1; // util.getLayerHdl(prj.LyrDEMname, map);
            if (iDemLyr != -1 & prj.DemChecked == true)
            {
                demLyr = map.Layers[iDemLyr] as IMapRasterLayer;
                int mRow = demLyr.Bounds.NumRows;
                int mCol = demLyr.Bounds.NumColumns;
                dem4Pv = (Raster)demLyr.DataSet;
                //Coordinate ptReference = new Coordinate(Convert.ToDouble(txtUtmE.Text), Convert.ToDouble(txtUtmN.Text));
                Coordinate ptReference = new Coordinate(x0, y0);
                RcIndex rc = dem4Pv.ProjToCell(ptReference);
                if (rc.Column < 0 | rc.Row < 0)
                {
                    z0 = 0;
                }
                else
                {
                    //double z0 = demLyr.DataSet.Value[rc.Row, rc.Column];
                    z0 = 0; //assume Building base level is equal to reference point level
                    //z0 = dem4Pv.Value[rc.Row, rc.Column];
                }
            }

            int nNode = 0;
            for (int iShp = 0; iShp < bldgFe.DataSet.NumRows(); iShp++)
            {
                IFeature fs = bldgFe.DataSet.GetFeature(iShp);
                nNode += fs.NumPoints;
            }
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            //object obj; 
            //XmlWriter writer = XmlWriter.Create(pvDir + "\\SketchUp\\temp.xml", settings);
            XmlWriter writer = XmlWriter.Create(temp_path + "\\temp.xml", settings);
            //-------------------------------------------------------
            //Header
            //-------------------------------------------------------
            writer.WriteStartElement("COLLADA");
            {
                writer.WriteAttributeString("xmlns", "xsi", null, "http://www.collada.org/2005/11/COLLADASchema");
                writer.WriteAttributeString("version", "1.4.1");
                writer.WriteStartElement("asset");
                {
                    writer.WriteStartElement("contributor");
                    {
                        writer.WriteStartElement("authoring_tool");
                        {
                            writer.WriteString("SketchUp 8.0.16846");
                        } writer.WriteEndElement(); //"authoring_tool"
                    } writer.WriteEndElement(); //"contributor"
                    writer.WriteStartElement("created");
                    {
                        writer.WriteString("2013-07-22T15:46:40Z");
                    } writer.WriteEndElement(); //"created"                    
                    writer.WriteStartElement("modified");
                    {
                        writer.WriteString("2013-07-22T15:46:40Z");
                    } writer.WriteEndElement(); //"modified"
                    writer.WriteStartElement("unit");
                    {
                        writer.WriteAttributeString("meter", "0.2539999969303608");
                        writer.WriteAttributeString("name", "inch");
                    } writer.WriteEndElement(); //"unit"
                    writer.WriteStartElement("up_axis");
                    {
                        writer.WriteString("Z_UP");
                    } writer.WriteEndElement(); //"up_axis"                    
                } writer.WriteEndElement(); //"asset"
                writer.WriteStartElement("library_visual_scenes");
                {
                    writer.WriteStartElement("visual_scene");
                    {
                        writer.WriteAttributeString("id", "ID1");
                        //--------------------------------------------------------------------------
                        // Node
                        //--------------------------------------------------------------------------
                        writer.WriteStartElement("node");
                        {
                            writer.WriteAttributeString("name", "SketchUp");
                            for (int iNode = 0; iNode < nNode; iNode++)
                            {
                                writer.WriteStartElement("instance_geometry");
                                {
                                    writer.WriteAttributeString("url", "#ID" + getSketchUpId(iNode).ToString());
                                    writer.WriteStartElement("bind_material");
                                    {
                                        writer.WriteStartElement("technique_common");
                                        {
                                            writer.WriteStartElement("instance_material");
                                            {
                                                writer.WriteAttributeString("symbol", "Material2");
                                                writer.WriteAttributeString("target", "#ID3");
                                                writer.WriteStartElement("bind_vertex_input");
                                                {
                                                    writer.WriteAttributeString("semantic", "UVSET0");
                                                    writer.WriteAttributeString("input_semantic", "TEXCOORD");
                                                    writer.WriteAttributeString("input_set", "0");
                                                } writer.WriteEndElement();
                                            } writer.WriteEndElement();
                                        } writer.WriteEndElement();
                                    } writer.WriteEndElement();
                                } writer.WriteEndElement();
                            }
                        } writer.WriteEndElement();//"Node"
                        //--------------------------------------------------------------------------
                    } writer.WriteEndElement(); //"visual_scene"
                } writer.WriteEndElement(); //"library_visual_scenes"
                //----------------------------------------------------------------------------------
                // Geometry
                //----------------------------------------------------------------------------------
                writer.WriteStartElement("library_geometries");
                double ToMeter = 3.96874995203687;
                /*
                double w = Convert.ToDouble(txtPvWidth.Text) * ToMeter;
                double h = Convert.ToDouble(txtPvLength.Text) * ToMeter;
                double az = pvAz.AzimutAngle;
                double tilt = pvTilt.tiltAngle;
                double xx0 = Convert.ToDouble(txtUtmE.Text);
                double yy0 = Convert.ToDouble(txtUtmN.Text);
                */
                int i = -1;
                for (int iShp = 0; iShp < bldgFe.DataSet.NumRows(); iShp++)
                {
                    IFeature fs = bldgFe.DataSet.GetFeature(iShp);
                    object val = fs.DataRow["height"];
                    double bldgEle = Convert.ToDouble(val);
                    Coordinate[] Side = new Coordinate[4];
                    for (int iSeg = 0; iSeg < fs.NumPoints; iSeg++)
                    {
                        i++;
                        if (iSeg < fs.NumPoints - 1)
                        {
                            Side[0] = new Coordinate((fs.Coordinates[iSeg + 1].X - x0) * ToMeter, (fs.Coordinates[iSeg + 1].Y - y0) * ToMeter, (z0) * ToMeter);
                            Side[1] = new Coordinate((fs.Coordinates[iSeg].X - x0) * ToMeter, (fs.Coordinates[iSeg].Y - y0) * ToMeter, (z0 + bldgEle) * ToMeter);
                            Side[2] = new Coordinate((fs.Coordinates[iSeg].X - x0) * ToMeter, (fs.Coordinates[iSeg].Y - y0) * ToMeter, (z0) * ToMeter);
                            Side[3] = new Coordinate((fs.Coordinates[iSeg + 1].X - x0) * ToMeter, (fs.Coordinates[iSeg + 1].Y - y0) * ToMeter, (z0 + bldgEle) * ToMeter);
                        }
                        else
                        {
                            Side[0] = new Coordinate((fs.Coordinates[0].X - x0) * ToMeter, (fs.Coordinates[0].Y - y0) * ToMeter, (z0) * ToMeter);
                            Side[1] = new Coordinate((fs.Coordinates[iSeg].X - x0) * ToMeter, (fs.Coordinates[iSeg].Y - y0) * ToMeter, (z0 + bldgEle) * ToMeter);
                            Side[2] = new Coordinate((fs.Coordinates[iSeg].X - x0) * ToMeter, (fs.Coordinates[iSeg].Y - y0) * ToMeter, (z0) * ToMeter);
                            Side[3] = new Coordinate((fs.Coordinates[0].X - x0) * ToMeter, (fs.Coordinates[0].Y - y0) * ToMeter, (z0 + bldgEle) * ToMeter);
                        }

                        writer.WriteStartElement("geometry");
                        {
                            writer.WriteAttributeString("id", "ID" + getSketchUpId(i).ToString());
                            writer.WriteStartElement("mesh");
                            {
                                //--------
                                //1st Set
                                //--------
                                writer.WriteStartElement("source");
                                {
                                    writer.WriteAttributeString("id", "ID" + getSketchUpSourceId(i).ToString());
                                    writer.WriteStartElement("float_array");
                                    {
                                        writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 3).ToString());
                                        writer.WriteAttributeString("count", "12");
                                        //writer.WriteString(get3Dcord(i));
                                        string p1 = Side[0].X.ToString() + " " + Side[0].Y.ToString() + " " + Side[0].Z.ToString();
                                        string p2 = Side[1].X.ToString() + " " + Side[1].Y.ToString() + " " + Side[1].Z.ToString();
                                        string p3 = Side[2].X.ToString() + " " + Side[2].Y.ToString() + " " + Side[2].Z.ToString();
                                        string p4 = Side[3].X.ToString() + " " + Side[3].Y.ToString() + " " + Side[3].Z.ToString();
                                        writer.WriteString(p1 + " " + p2 + " " + p3 + " " + p4);

                                    } writer.WriteEndElement();//"float_array"    

                                    writer.WriteStartElement("technique_common");
                                    {
                                        writer.WriteStartElement("accessor");
                                        {// <accessor count="24" source="#ID8" stride="3">
                                            writer.WriteAttributeString("count", "4");
                                            writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 3).ToString());
                                            writer.WriteAttributeString("stride", "3");
                                            //<param name="X" type="float" />
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "X");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Y");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Z");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement(); //"param"
                                        } writer.WriteEndElement(); //"accessor"
                                    } writer.WriteEndElement(); //"technique_common"
                                } writer.WriteEndElement();//"source"
                                //--------
                                //2nd Set
                                //--------
                                writer.WriteStartElement("source");
                                {
                                    writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 1).ToString());
                                    writer.WriteStartElement("float_array");
                                    {
                                        writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 4).ToString());
                                        writer.WriteAttributeString("count", "12");
                                        // writer.WriteString("0 0 -1 0 0 -1 0 0 -1 0 0 -1 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -0 1 0 -0 1 0 -0 1 0 -0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 -0 -1 -0 -0 -1 -0 -0 -1 -0 -0 -1 -0 0 0 1 0 0 1 0 0 1 0 0 1");
                                        writer.WriteString(getSketchUp3DCam(1));
                                    } writer.WriteEndElement();//"float_array"    

                                    writer.WriteStartElement("technique_common");
                                    {
                                        writer.WriteStartElement("accessor");
                                        {// <accessor count="24" source="#ID8" stride="3">
                                            writer.WriteAttributeString("count", "4");
                                            writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 4).ToString());
                                            writer.WriteAttributeString("stride", "3");
                                            //<param name="X" type="float" />
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "X");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Y");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement();//"param"
                                            writer.WriteStartElement("param");
                                            {
                                                writer.WriteAttributeString("name", "Z");
                                                writer.WriteAttributeString("type", "float");
                                            } writer.WriteEndElement(); //"param"
                                        } writer.WriteEndElement(); //"accessor"
                                    } writer.WriteEndElement(); //"technique_common"
                                } writer.WriteEndElement();//"source"
                                //--------
                                //3rd Set
                                //--------
                                writer.WriteStartElement("vertices");
                                {
                                    writer.WriteAttributeString("id", "ID" + (getSketchUpSourceId(i) + 2).ToString());
                                    writer.WriteStartElement("input");
                                    {// <input semantic="POSITION" source="#ID11" />
                                        writer.WriteAttributeString("semantic", "POSITION");
                                        writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 0).ToString());
                                    } writer.WriteEndElement(); //"input"
                                    writer.WriteStartElement("input");
                                    {// <input semantic="NORMAL" source="#ID12" />
                                        writer.WriteAttributeString("semantic", "NORMAL");
                                        writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 1).ToString());
                                    } writer.WriteEndElement(); //"input"
                                } writer.WriteEndElement();//"vertices"
                                writer.WriteStartElement("triangles");
                                {//<triangles count="12" material="Material2">
                                    writer.WriteAttributeString("count", "2");
                                    writer.WriteAttributeString("material", "Material2");
                                    writer.WriteStartElement("input");
                                    {//<input offset="0" semantic="VERTEX" source="#ID13" />
                                        writer.WriteAttributeString("offset", "0");
                                        writer.WriteAttributeString("semantic", "VERTEX");
                                        writer.WriteAttributeString("source", "#ID" + (getSketchUpSourceId(i) + 2).ToString());
                                    } writer.WriteEndElement(); //"input"
                                    writer.WriteStartElement("p");
                                    {//
                                        writer.WriteString("0 1 2 1 0 3");
                                    } writer.WriteEndElement(); //"p"

                                } writer.WriteEndElement();//"triangles"                        
                            } writer.WriteEndElement();//"mesh"
                        } writer.WriteEndElement(); //"geometry"
                    } //Segment
                } writer.WriteEndElement();//library_geometries
                //----------------------------------------------------------------------------------
                //Footer
                //----------------------------------------------------------------------------------
                writer.WriteStartElement("library_materials");
                {
                    writer.WriteStartElement("material");
                    {
                        writer.WriteAttributeString("id", "ID3");
                        writer.WriteAttributeString("name", "__auto_");
                        writer.WriteStartElement("instance_effect");
                        {
                            writer.WriteAttributeString("url", "#ID4");
                        } writer.WriteEndElement(); //"instance_effect"
                    } writer.WriteEndElement(); //"material"
                } writer.WriteEndElement(); //"library_materials"

                writer.WriteStartElement("library_effects");
                {
                    writer.WriteStartElement("effect");
                    {
                        writer.WriteAttributeString("id", "ID4");
                        writer.WriteStartElement("profile_COMMON");
                        {
                            writer.WriteStartElement("technique");
                            {
                                writer.WriteAttributeString("sid", "COMMON");
                                writer.WriteStartElement("lambert");
                                {
                                    writer.WriteStartElement("diffuse");
                                    {
                                        writer.WriteStartElement("color");
                                        {
                                            writer.WriteString("1 1 1 1");

                                        } writer.WriteEndElement(); //"color"   
                                    } writer.WriteEndElement(); //"diffuse"   
                                } writer.WriteEndElement(); //"lambert"      
                            } writer.WriteEndElement(); //"technique"      
                        } writer.WriteEndElement(); //"profile_COMMON"       
                    } writer.WriteEndElement(); //"effect"                
                } writer.WriteEndElement(); //"library_effects"

                writer.WriteStartElement("scene");
                {
                    writer.WriteStartElement("instance_visual_scene");
                    {
                        writer.WriteAttributeString("url", "#ID1");

                    } writer.WriteEndElement(); //"instance_visual_scene"   
                } writer.WriteEndElement(); //"scene"   
            } writer.WriteEndElement(); //"COLLADA"
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();



            String line;
            try
            {
                StreamReader sh = new StreamReader(pvDir + "\\SketchUp\\COLLADASH.h");
                //StreamReader sr = new StreamReader(pvDir + "\\SketchUp\\temp.xml");
                StreamReader sr = new StreamReader(temp_path + "\\temp.xml");
                StreamWriter sw = new StreamWriter(exportfolder + "\\pvBuilding.dae");
                line = sh.ReadLine(); sw.WriteLine(line); line = sr.ReadLine();
                line = sh.ReadLine(); sw.WriteLine(line); line = sr.ReadLine();
                line = sr.ReadLine();
                while (line != null)
                {
                    sw.WriteLine(line);
                    line = sr.ReadLine();
                }

                //close the file
                sh.Close();
                sr.Close();
                sw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }

        public void ExportBldgAndTrr2SketchUp_Click(object sender, EventArgs e)
        {

            //pvVerify();
            //if (verify[0] == false)
            //{
            //    MessageBox.Show("Please assign a reference location before exporting data to SketchUp");
            //    return;
            //}

            FolderBrowserDialog folderSel = new FolderBrowserDialog();
            folderSel.Description = "Select file location to export Sketchup files:";
            //folderSel.ShowDialog();
            DialogResult result = folderSel.ShowDialog();
            if (result == DialogResult.OK)
            {
                if (folderSel.SelectedPath != null)
                {

                    //Export2SketchUp4PvPanel
                    int treeLyr = -1; // util.getLayerHdl(prj.LyrTreeName, map);  // TODO - UNDO DISABLED TREE CHECK
                    if (treeLyr != -1)
                    {
                        IMapFeatureLayer treeFs = map.Layers[treeLyr] as IMapFeatureLayer;
                        Export2SketchUp4Tree(treeFs, folderSel.SelectedPath);
                    }
                    else
                    {
                        MessageBox.Show("Cannot export tree model. Please select tree layer first.");
                    }
                    //Export2SketchUp4Building
                    int bldgLyr = util.getLayerHdl(prj.LyrBuildingName, map);
                    if (bldgLyr != -1)
                    {
                        IMapFeatureLayer bldgFs = map.Layers[bldgLyr] as IMapFeatureLayer;
                        Export2SketchUp4Bldg(bldgFs, folderSel.SelectedPath);
                    }
                    else
                    {
                        MessageBox.Show("Cannot export building model. Please select building layer first.");
                    }
                    MessageBox.Show("Google SketchUp file export completed");
                }
                else
                {
                    MessageBox.Show("Select file location to export Sketchup files");
                }
            }
            else
            {
                result = DialogResult.Cancel;
            }


        }

        string GeoTree4SketchUp(double radius, double scaleZ, double shiftX, double shiftY, double shiftZ)
        {
            double[] xPt = new double[24];
            double[] yPt = new double[24];
            double[] zPt = new double[24];
            xPt[0] = -149.828; yPt[0] = -48.492; zPt[0] = 0;
            xPt[1] = -154.001; yPt[1] = 32.919; zPt[1] = 0;
            xPt[2] = -157.274; yPt[2] = -8.062; zPt[2] = 0;
            xPt[3] = -140.234; yPt[3] = 71.655; zPt[3] = 0;
            xPt[4] = -132.172; yPt[4] = -85.618; zPt[4] = 0;
            xPt[5] = -116.91; yPt[5] = 105.509; zPt[5] = 0;
            xPt[6] = -105.509; yPt[6] = -116.91; zPt[6] = 0;
            xPt[7] = -85.618; yPt[7] = 132.172; zPt[7] = 0;
            xPt[8] = -71.655; yPt[8] = -140.234; zPt[8] = 0;
            xPt[9] = -48.492; yPt[9] = 149.828; zPt[9] = 0;
            xPt[10] = -32.919; yPt[10] = -154.001; zPt[10] = 0;
            xPt[11] = -8.062; yPt[11] = 157.274; zPt[11] = 0;
            xPt[12] = 8.062; yPt[12] = -157.274; zPt[12] = 0;
            xPt[13] = 32.919; yPt[13] = 154.001; zPt[13] = 0;
            xPt[14] = 48.492; yPt[14] = -149.828; zPt[14] = 0;
            xPt[15] = 71.655; yPt[15] = 140.234; zPt[15] = 0;
            xPt[16] = 85.618; yPt[16] = -132.172; zPt[16] = 0;
            xPt[17] = 105.509; yPt[17] = 116.91; zPt[17] = 0;
            xPt[18] = 116.91; yPt[18] = -105.509; zPt[18] = 0;
            xPt[19] = 132.172; yPt[19] = 85.618; zPt[19] = 0;
            xPt[20] = 140.234; yPt[20] = -71.655; zPt[20] = 0;
            xPt[21] = 149.828; yPt[21] = 48.492; zPt[21] = 0;
            xPt[22] = 154.001; yPt[22] = -32.919; zPt[22] = 0;
            xPt[23] = 157.274; yPt[23] = 8.062; zPt[23] = 0;
            string Cord72 = "";
            double x, y, z;
            for (int i = 0; i <= 23; i++)
            {
                x = xPt[i] * radius + shiftX;
                y = yPt[i] * radius + shiftY;
                z = zPt[i] * scaleZ + shiftZ;
                if (i == 23)
                {
                    Cord72 = Cord72 + x + " " + y + " " + z;
                }
                else
                {
                    Cord72 = Cord72 + x + " " + y + " " + z + " ";
                }
            }

            return Cord72;
        }

        string getSketchUp3DCam(int i)
        {
            string c;
            c = "0 0 -1 0 0 -1 0 0 -1 0 0 -1 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -0 1 0 -0 1 0 -0 1 0 -0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 -0 -1 -0 -0 -1 -0 -0 -1 -0 -0 -1 -0 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1";
            c = "0 -1 1 0 -1 1 0 -1 1 0 -1 1";
            return c;
        }

        int getTreeTypeId(string treeTypeName)
        {
            int id = -1;
            if (treeTypeName.ToUpper() == "Spreading".ToUpper()) id = 0;
            if (treeTypeName.ToUpper() == "Round".ToUpper()) id = 1;
            if (treeTypeName.ToUpper() == "Pyramidal".ToUpper()) id = 2;
            if (treeTypeName.ToUpper() == "Oval".ToUpper()) id = 3;
            if (treeTypeName.ToUpper() == "Conical".ToUpper()) id = 4;
            if (treeTypeName.ToUpper() == "Vase".ToUpper()) id = 5;
            if (treeTypeName.ToUpper() == "Columnar".ToUpper()) id = 6;
            if (treeTypeName.ToUpper() == "Open".ToUpper()) id = 7;
            if (treeTypeName.ToUpper() == "Weeping".ToUpper()) id = 8;
            if (treeTypeName.ToUpper() == "Irrigular".ToUpper()) id = 9;
            return id;
        }

        double[,] treeShape = new double[10, 2];
        double[] whRatio = new double[10];
        string[] treeTypeName = new string[10];

        void setTreeShape(int TreeForm)
        {
            whRatio[0] = 1.21; treeTypeName[0] = "Speading shape";
            whRatio[1] = 1.84; treeTypeName[1] = "Round shape";
            whRatio[2] = 2.04; treeTypeName[2] = "Pyramidal shape";
            whRatio[3] = 3.91; treeTypeName[3] = "Oval shape";
            whRatio[4] = 5.52; treeTypeName[4] = "Conical shape";
            whRatio[5] = 1.56; treeTypeName[5] = "Vase shape";
            whRatio[6] = 12.5; treeTypeName[6] = "Columnar shape";
            whRatio[7] = 2.63; treeTypeName[7] = "Open shape";
            whRatio[8] = 2.29; treeTypeName[8] = "Weeping shape";
            whRatio[9] = 3.43; treeTypeName[9] = "Irrigular shape";
            if (TreeForm == 1)
            {
                //Form1 Speading
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.0566037735849057;
                treeShape[1, 0] = 0.154411764705882; treeShape[1, 1] = 0.0660377358490566;
                treeShape[2, 0] = 0.198529411764706; treeShape[2, 1] = 0.405660377358491;
                treeShape[3, 0] = 0.227941176470588; treeShape[3, 1] = 0.679245283018868;
                treeShape[4, 0] = 0.308823529411765; treeShape[4, 1] = 0.905660377358491;
                treeShape[5, 0] = 0.470588235294118; treeShape[5, 1] = 1;
                treeShape[6, 0] = 0.647058823529412; treeShape[6, 1] = 0.905660377358491;
                treeShape[7, 0] = 0.779411764705882; treeShape[7, 1] = 0.707547169811321;
                treeShape[8, 0] = 0.875; treeShape[8, 1] = 0.471698113207547;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 2)
            {
                //Form02 Round
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.0847457627118644;
                treeShape[1, 0] = 0.208695652173913; treeShape[1, 1] = 0.0508474576271186;
                treeShape[2, 0] = 0.304347826086957; treeShape[2, 1] = 0.271186440677966;
                treeShape[3, 0] = 0.339130434782609; treeShape[3, 1] = 0.644067796610169;
                treeShape[4, 0] = 0.408695652173913; treeShape[4, 1] = 0.949152542372881;
                treeShape[5, 0] = 0.547826086956522; treeShape[5, 1] = 1;
                treeShape[6, 0] = 0.71304347826087; treeShape[6, 1] = 0.915254237288136;
                treeShape[7, 0] = 0.852173913043478; treeShape[7, 1] = 0.677966101694915;
                treeShape[8, 0] = 0.947826086956522; treeShape[8, 1] = 0.372881355932203;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 3)
            {
                //Form03 Pyramidal
                treeShape[0, 0] = 0; treeShape[0, 1] = 1;
                treeShape[1, 0] = 0.17741935483871; treeShape[1, 1] = 0.661290322580645;
                treeShape[2, 0] = 0.193548387096774; treeShape[2, 1] = 0.661290322580645;
                treeShape[3, 0] = 0.443548387096774; treeShape[3, 1] = 0.467741935483871;
                treeShape[4, 0] = 0.459677419354839; treeShape[4, 1] = 0.596774193548387;
                treeShape[5, 0] = 0.67741935483871; treeShape[5, 1] = 0.225806451612903;
                treeShape[6, 0] = 0.685483870967742; treeShape[6, 1] = 0.354838709677419;
                treeShape[7, 0] = 0.82258064516129; treeShape[7, 1] = 0.0967741935483871;
                treeShape[8, 0] = 0.82258064516129; treeShape[8, 1] = 0.193548387096774;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 4)
            {
                //Form04 Oval
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.268292682926829;
                treeShape[1, 0] = 0.136363636363636; treeShape[1, 1] = 0.0975609756097561;
                treeShape[2, 0] = 0.220779220779221; treeShape[2, 1] = 0.0731707317073171;
                treeShape[3, 0] = 0.279220779220779; treeShape[3, 1] = 0.585365853658537;
                treeShape[4, 0] = 0.357142857142857; treeShape[4, 1] = 0.951219512195122;
                treeShape[5, 0] = 0.512987012987013; treeShape[5, 1] = 1;
                treeShape[6, 0] = 0.616883116883117; treeShape[6, 1] = 0.853658536585366;
                treeShape[7, 0] = 0.74025974025974; treeShape[7, 1] = 0.926829268292683;
                treeShape[8, 0] = 0.974025974025974; treeShape[8, 1] = 0.341463414634146;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 5)
            {
                //Form05 Conical
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.571428571428571;
                treeShape[1, 0] = 0.0625; treeShape[1, 1] = 0.964285714285714;
                treeShape[2, 0] = 0.178571428571429; treeShape[2, 1] = 1;
                treeShape[3, 0] = 0.375; treeShape[3, 1] = 0.785714285714286;
                treeShape[4, 0] = 0.544642857142857; treeShape[4, 1] = 0.571428571428571;
                treeShape[5, 0] = 0.660714285714286; treeShape[5, 1] = 0.392857142857143;
                treeShape[6, 0] = 0.767857142857143; treeShape[6, 1] = 0.214285714285714;
                treeShape[7, 0] = 0.839285714285714; treeShape[7, 1] = 0.107142857142857;
                treeShape[8, 0] = 0.910714285714286; treeShape[8, 1] = 0;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 6)
            {
                //Form06 Vese
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.106060606060606;
                treeShape[1, 0] = 0.151785714285714; treeShape[1, 1] = 0.0606060606060606;
                treeShape[2, 0] = 0.25; treeShape[2, 1] = 0.181818181818182;
                treeShape[3, 0] = 0.276785714285714; treeShape[3, 1] = 0.378787878787879;
                treeShape[4, 0] = 0.401785714285714; treeShape[4, 1] = 0.636363636363636;
                treeShape[5, 0] = 0.517857142857143; treeShape[5, 1] = 0.863636363636364;
                treeShape[6, 0] = 0.651785714285714; treeShape[6, 1] = 1;
                treeShape[7, 0] = 0.785714285714286; treeShape[7, 1] = 0.893939393939394;
                treeShape[8, 0] = 0.9375; treeShape[8, 1] = 0.484848484848485;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 7)
            {
                //Form07 Columnar
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.258064516129032;
                treeShape[1, 0] = 0.111111111111111; treeShape[1, 1] = 0.161290322580645;
                treeShape[2, 0] = 0.163398692810458; treeShape[2, 1] = 0.451612903225806;
                treeShape[3, 0] = 0.215686274509804; treeShape[3, 1] = 0.870967741935484;
                treeShape[4, 0] = 0.320261437908497; treeShape[4, 1] = 1;
                treeShape[5, 0] = 0.477124183006536; treeShape[5, 1] = 0.870967741935484;
                treeShape[6, 0] = 0.61437908496732; treeShape[6, 1] = 0.838709677419355;
                treeShape[7, 0] = 0.718954248366013; treeShape[7, 1] = 0.741935483870968;
                treeShape[8, 0] = 0.869281045751634; treeShape[8, 1] = 0.548387096774194;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 8)
            {
                //Form08 Open
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.0925925925925926;
                treeShape[1, 0] = 0.18796992481203; treeShape[1, 1] = 0.0555555555555556;
                treeShape[2, 0] = 0.218045112781955; treeShape[2, 1] = 0.777777777777778;
                treeShape[3, 0] = 0.293233082706767; treeShape[3, 1] = 0.981481481481482;
                treeShape[4, 0] = 0.406015037593985; treeShape[4, 1] = 1;
                treeShape[5, 0] = 0.511278195488722; treeShape[5, 1] = 0.981481481481482;
                treeShape[6, 0] = 0.714285714285714; treeShape[6, 1] = 0.814814814814815;
                treeShape[7, 0] = 0.834586466165414; treeShape[7, 1] = 0.685185185185185;
                treeShape[8, 0] = 0.932330827067669; treeShape[8, 1] = 0.333333333333333;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }
            if (TreeForm == 9)
            {
                //Form09 Weeping
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.103448275862069;
                treeShape[1, 0] = 0.0839160839160839; treeShape[1, 1] = 0.103448275862069;
                treeShape[2, 0] = 0.125874125874126; treeShape[2, 1] = 0.741379310344828;
                treeShape[3, 0] = 0.230769230769231; treeShape[3, 1] = 0.913793103448276;
                treeShape[4, 0] = 0.461538461538462; treeShape[4, 1] = 1;
                treeShape[5, 0] = 0.678321678321678; treeShape[5, 1] = 0.793103448275862;
                treeShape[6, 0] = 0.811188811188811; treeShape[6, 1] = 0.586206896551724;
                treeShape[7, 0] = 0.916083916083916; treeShape[7, 1] = 0.379310344827586;
                treeShape[8, 0] = 0.972027972027972; treeShape[8, 1] = 0.189655172413793;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;

            }
            if (TreeForm == 10)
            {
                //Form10 Irrigular
                treeShape[0, 0] = 0; treeShape[0, 1] = 0.0566037735849057;
                treeShape[1, 0] = 0.154411764705882; treeShape[1, 1] = 0.0660377358490566;
                treeShape[2, 0] = 0.198529411764706; treeShape[2, 1] = 0.405660377358491;
                treeShape[3, 0] = 0.227941176470588; treeShape[3, 1] = 0.679245283018868;
                treeShape[4, 0] = 0.308823529411765; treeShape[4, 1] = 0.905660377358491;
                treeShape[5, 0] = 0.470588235294118; treeShape[5, 1] = 1;
                treeShape[6, 0] = 0.647058823529412; treeShape[6, 1] = 0.905660377358491;
                treeShape[7, 0] = 0.779411764705882; treeShape[7, 1] = 0.707547169811321;
                treeShape[8, 0] = 0.875; treeShape[8, 1] = 0.471698113207547;
                treeShape[9, 0] = 1; treeShape[9, 1] = 0;
            }

        }

        double treeRedius(double z, int TreeForm)
        {
            setTreeShape(TreeForm);
            double r = 0.001;
            for (int i = 0; i < 9; i++)
            {
                if (treeShape[i, 0] <= z & z < treeShape[i + 1, 0])
                {
                    double x1 = treeShape[i, 0];
                    double y1 = treeShape[i, 1];
                    double x2 = treeShape[i + 1, 0];
                    double y2 = treeShape[i + 1, 1];
                    r = (y2 - y1) / (x2 - x1) * (z - x1) + y1;
                }
            }
            if (r == 0) r = 0.001;
            return r / 40;
        }
    }
}
